define(['app'], function(app) {
	'use strict';

	var controller = function controller($scope, Distributor_publish,GoDialog, GoAddress,$stateParams,$window,GoCatTypeAuth,Auth,GoPage) {
		$scope.scope = new Object();
		var brand = new Array();
		var cat_type = new Array();
		var regions = new Array();
		$scope.data_cat = new Array();
		$scope.cats = new Array();
		$scope.regions = new Array();
		$scope.input = new Object();
		$scope.input.brand = {id:0,name:"请选择品牌"};
		$scope.cat1 = new Array();
		$scope.cat2 = new Array();
		$scope.cat3 = new Array();
		var change_itmes1 = new Array();
		var change_itmes2 = new Array();
		var change_itmes3 = new Array();
		var arr = new Array();
		var cat = null;
		var cat_arr = new Array();
		var region =null;
		$scope.scope = new Object();
			if($stateParams.info_id != ""){
			$scope.info_id = $stateParams.info_id;	
		}
		//画面加载
		$scope.$on('$ionicView.afterEnter', function() {
			//动态设置高度
			var all = $window.innerHeight;
			//计算滚动条
			$scope.myObj = {
				"height": all - 44 + "px"
			};
			$scope.init_data();
		}, false);
		/***省市区三级联动***/
		$scope.input.province = { id: 0, name: "请选择省份" }; //省市区三级联动
		$scope.input.city = { id: 0, name: "请选择城市" }; //省市区三级联动
		$scope.input.district = { id: 0, name: "请选择区/县" }; //省市区三级联动

		//下拉侦听，目前只能省市区三级联动
		$scope.listen_province = function(data, obj) {
			$scope.input.city = { id: 0, name: "请选择城市" }; //省市区三级联动
			$scope.input.district = { id: 0, name: "请选择区/县" }; //省市区三级联动
			$scope.district_items = null;
			if($scope.is_city == true) {
				for(var i = 0; i < $scope.city_items.length; i++) {
						if($scope.input.city_name == $scope.city_items[i].name) {
							$scope.input.city = $scope.city_items[i];
							$scope.is_city = false;
							break;
						}
					}
			}
			
		};
		$scope.listen_city = function(data, obj) {
			$scope.input.district = { id: 0, name: "请选择区/县" }; //省市区三级联动
			if($scope.is_district == true) {
				for(var i = 0; i < $scope.district_items.length; i++) {
						if($scope.input.district_name == $scope.district_items[i].name) {
							$scope.input.district = $scope.district_items[i];
							$scope.is_district = false;
							break;
						}

					}
					
			}
		};
		$scope.doSelectProvince = function () {
			//id是值 name是显示
			GoDialog.showSelect("请选择省份", $scope.province_items, $scope.input.province, function (data) {
				$scope.input.province = data;
			});
		}
		$scope.doSelectCity = function () {
			//id是值 name是显示
			GoDialog.showSelect("请选择城市", $scope.city_items, $scope.input.city, function (data) {
				$scope.input.city = data;
			});
		}
		$scope.doSelectDistrict = function () {
			//id是值 name是显示
			GoDialog.showSelect("请选择区/县", $scope.district_items, $scope.input.district, function (data) {
				$scope.input.district = data;
			});
		}
		$scope.doSelectStore = function () {
			//id是值 name是显示
			//id是值 name是显示
			var list = new Array();
			$scope.datas.data.forEach(obj => {
				if (obj.id == undefined)
					obj.id = obj.store_id;
				if (obj.name == undefined)
					obj.name = obj.store_name;
				list.push(obj);
			});

			GoDialog.showSelect("请选择请选择店铺", list, $scope.input.store_select, function (data) {
				$scope.input.store_id = data.store_id;
				$scope.input.store_select = data;
			});
		}

		//定义绑定
		var items = new Array();
		var obj1 = { name: 'input.province' };
		var obj2 = { name: 'input.city' };
		var obj3 = { name: 'input.district' };
		items.push(obj1);
		items.push(obj2);
		items.push(obj3);
		GoAddress.init_load($scope, items);

		/***分类三级联动***/
		$scope.input.store_id = Auth.get_store_id(); //隐藏的企业id的绑定数据
		$scope.input.brand = {id:0,name:"请选择品牌"};
		$scope.input.cat1 = {id:0,name:"请选择大分类"};
		$scope.input.cat2 = {id:0,name:"请选择中分类"};
		$scope.input.cat3 = {id:0,name:"请选择小分类"};
		//下拉侦听
		$scope.listen_brand = function(data, obj) {
			$scope.input.cat1 = {id:0,name:"请选择大分类"};
			$scope.input.cat2 = {id:0,name:"请选择中分类"};
			$scope.input.cat3 = {id:0,name:"请选择小分类"};
			if($scope.is_cat1==true){
				for(var i=0;i<$scope.cat1_items.length;i++){
					if($scope.cat1 == $scope.cat1_items[i].name){
						$scope.input.cat1 = $scope.cat1_items[i];
						$scope.is_cat1 = false;
					}
				}
			}
			
			
		};
		$scope.listen_cat1 = function(data, obj) {
			$scope.input.cat2 = {id:0,name:"请选择中分类"};
			$scope.input.cat3 = {id:0,name:"请选择小分类"};
			$scope.cat3_items = null;
			if($scope.is_cat2==true){
				for(var i=0;i<$scope.cat2_items.length;i++){
					if($scope.cat2 == $scope.cat2_items[i].name){
						$scope.input.cat2 = $scope.cat2_items[i];
						$scope.is_cat2 = false;
					}
				}
			}
		};
		$scope.listen_cat2 = function(data, obj) {
			$scope.input.cat3 = {id:0,name:"请选择小分类"};
			if($scope.is_cat3==true){
				for(var i=0;i<$scope.cat3_items.length;i++){
					if($scope.cat3 == $scope.cat3_items[i].name){
						$scope.input.cat3 = $scope.cat3_items[i];
						$scope.is_cat3 = false;
					}
				}
			}
		};
		//定义绑定
		var items_cat = new Array();
		var obj_brand = { name: 'input.brand' };
		var obj1_cat = { name: 'input.cat1' };
		var obj2_cat = { name: 'input.cat2' };
		var obj3_cat = { name: 'input.cat3' };
		items_cat.push(obj_brand);
		items_cat.push(obj1_cat);
		items_cat.push(obj2_cat);
		items_cat.push(obj3_cat);
		GoCatTypeAuth.init_load($scope, items_cat);
		$scope.doSelectbrand = function(){
			//id是值 name是显示
			GoDialog.showSelect("大分类",$scope.brand_items,$scope.input.brand,function(data){
				$scope.input.brand = data;
			});
		}
		$scope.doSelectCat1 = function(){
			//id是值 name是显示
			GoDialog.showSelect("大分类",$scope.cat1_items,$scope.input.cat1,function(data){
				$scope.input.cat1 = data;
			});
		}
		$scope.doSelectCat2 = function(){
			//id是值 name是显示
			GoDialog.showSelect("中分类",$scope.cat2_items,$scope.input.cat2,function(data){
				$scope.input.cat2 = data;
			});
		}
		$scope.doSelectCat3 = function(){
			//id是值 name是显示
			GoDialog.showSelect("小分类",$scope.cat3_items,$scope.input.cat3,function(data){
				$scope.input.cat3 = data;
			});
		}
		$scope.scope.brand_id = null;
		$scope.scope.num = null;
		$scope.scope.contact = null;
		$scope.scope.requirement = null;
		$scope.input_region_data = function() {
			if($scope.input.province!=null){
				region =$scope.input.province.name+':'+$scope.input.province.id;
				$scope.region = $scope.input.province.name;
			}else{
				
			}
			if($scope.input.city!=null){
				$scope.region =$scope.input.province.name+'/'+ $scope.input.city.name;
				region =$scope.region+':'+ $scope.input.city.id;
				
			}
			if($scope.input.district!=null){
				$scope.region =$scope.input.province.name+'/'+$scope.input.city.name+'/'+ $scope.input.district.name;
				region =$scope.region+':'+ $scope.input.district.id;
				
			}
			var len = $scope.regions.length;
			for(var i = 0; i <= len; i++) {
					if($scope.regions[i]==$scope.region){
						return;
					}
				}
			$scope.regions[len] =$scope.region;
			regions[len] = {"id":region};
		}
		$scope.input_cat_data = function(){
			if($scope.input.cat1!=null){
				$scope.cat = $scope.input.cat1.name;
				cat =$scope.cat+':'+ $scope.input.cat1.id;
				
				}else{
					GoDialog.toast("请选择分类");
					return;
				}
			if($scope.input.cat2!=null){
				$scope.cat = $scope.input.cat1.name+'/'+$scope.input.cat2.name;
				cat =$scope.cat+':'+ $scope.input.cat2.id;
				
			}
			if($scope.input.cat3!=null){
				$scope.cat = $scope.input.cat1.name+'/'+$scope.input.cat2.name+'/'+$scope.input.cat3.name;
				cat =$scope.cat+':'+ $scope.input.cat3.id;
			}
			var len = $scope.cats.length;
			for(var i = 0; i <= len; i++) {
				if($scope.cats[i]==$scope.cat){
					return;
				}
			}
			$scope.cats[len] = $scope.cat;
			cat_type[len] = {"id":cat};
		}
		$scope.submit = function() {
			$scope.input_region_data();
			$scope.input_cat_data();
			if(!$scope.input.brand.id) {
				GoDialog.toast("请选择品牌");
				return;
			}
			if(cat_type.length<1) {

				GoDialog.toast("请输入品类");
				return;
			}
			if(regions.length<1) {
				GoDialog.toast("请输入地域");
				return;
			}
			if(!$scope.scope.requirement) {
				GoDialog.toast("请输入需求描述");
				return;
			}
			if(!$scope.scope.num) {
				GoDialog.toast("请输入预设数");
				return;
			} 
			if(isNaN($scope.scope.num)){
				GoDialog.toast("预设数不是数字");
				return;
			}
				Distributor_publish.distributordemand_edit($scope.info_id, $scope.input.brand.id, cat_type, regions, $scope.scope.requirement, $scope.scope.num, $scope.scope.contact, function(data) {
					if(data.success == true){
					GoDialog.toast("提交成功");	
					GoPage.go_states("distributor_list",{"is_init":true});
					}
						
					else
						GoDialog.toast(data.msg);
				});
		}
		$scope.delete_cat = function(val) {
			$scope.cats.splice(val, 1); //在val下标删除一个元素，后面的元素会自己填充这个下标
			cat_type.splice(val, 1);
		}
		$scope.delete_region = function(val) {
			$scope.regions.splice(val, 1); //在val下标删除一个元素，后面的元素会自己填充这个下标
			regions.splice(val, 1);
		}
		$scope.init_data = function(){
			if($scope.info_id != undefined){
				Distributor_publish.distributor_detail($scope.info_id,function(data){
					$scope.init_province(data);
					$scope.init_brand(data);
					$scope.scope.requirement = data.requirement;
					$scope.scope.contact =parseInt(data.phone);
					$scope.scope.num = parseInt(data.num);
					
				})
			}
		}
		//初始化接口中的省份
		$scope.init_province = function(data) {
			if(data.region == undefined)return;
//			console.log(data);
			arr = data.region[0].name.split(' > ');
			var i = arr.length;
			$scope.input.province_name = arr[0];
			$scope.input.city_name = arr[1];
			$scope.input.district_name = arr[2];
			if($scope.input.city_name)
			$scope.is_city = true;
			else
			$scope.is_city = false;
			if($scope.input.district_name)
			$scope.is_district = true;
			else
			$scope.is_district = false;
			GoAddress.init_htp($scope, 0, '', function(data) {
				for(var i = 0; i < $scope.province_items.length; i++) {
					if($scope.input.province_name == $scope.province_items[i].name) {
						$scope.input.province = $scope.province_items[i];
						break;
					}
				}
//				console.log($scope.input.province);
			})
		}
		
		$scope.init_brand = function(data){
			if(data.cat_type == undefined)return;
			$scope.brand_id = data.brand_id;
			cat_arr = data.cat_type[0].name.split(" > ");
			$scope.cat1 = cat_arr[0];
			$scope.cat2 = cat_arr[1];
			$scope.cat3 = cat_arr[2];
			if($scope.cat1)
			$scope.is_cat1 = true;
			else
			$scope.is_cat1 = false;
			if($scope.cat2)
			$scope.is_cat2 = true;
			else
			$scope.is_cat2 = false;
			if($scope.cat3)
			$scope.is_cat3 = true;
			else
			$scope.is_cat3 = false;
//			console.log($scope.brand_id);
			GoCatTypeAuth.init_brand($scope,function(data){
				for(var i =0;i<$scope.brand_items.length;i++){
					if($scope.brand_id.toString()==$scope.brand_items[i].id){
						$scope.input.brand = $scope.brand_items[i];
					}
				}
			})
		}
		
	}

	controller.$inject = ['$scope', 'Distributor_publish','GoDialog', 'GoAddress','$stateParams','$window','GoCatTypeAuth','Auth','GoPage'];

	return app.registerController("distributor_publishctrl",controller);
});